package com.example.administrator.nurse.widget;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.nurse.common;


/**
 * Created by LinGuanHong on 2015/11/23.
 */

public class LoginInputEdit extends EditText {

    public int numberLimit = 1; /** 最少输入字数限制 */
    private boolean isCan = false;
    private Context context;

    public void setNumberLimit(int numberLimit){
        this.numberLimit = numberLimit;
    }

    /** 设置密码输入 */
    public void setIsPassword(){
        this.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    public boolean isOk(){
        return isCan;
    }

    public boolean isOk(String str){
        if(isOk()){
            return true;
        }else{
            t(str);
        }
        return false;
    }

    public LoginInputEdit(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public LoginInputEdit(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public LoginInputEdit(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public void t(String str){
        Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
    }

    private CharSequence temp;
    private boolean temp1 = false ;
    public void setTextStr(CharSequence text){
        temp = text;
        temp1 = true;
        setText(text);
    }

    private void init(){
        this.addTextChangedListener(accountTextWatcher);
    }

    TextWatcher accountTextWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if(!temp1){
                temp=s;
            }else{
                temp1 = false;
            }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(temp!=null){
                if(temp.length() < numberLimit){
                    isCan=false;
                } else if(temp.length() >= numberLimit){
                    isCan=true;
                }
            }
        }

    };

    public String getStringText(String error){
        String target = this.getText().toString().trim();
        if(target.equals("")){ // edit is null
            // error info show
            Toast.makeText(common.context,error+"!",Toast.LENGTH_LONG).show();
            return null;
        }else{
            return target;
        }
    }

    public String getStringText(){
        String target = this.getText().toString().trim();
        if(target.equals("")){ // edit is null
            return null;
        }else{
            return target;
        }
    }
    //hhc 17.5.4 设置信息
    public void setStringText(String s){
        this.setText(s);
    }

//    public boolean checkBadWords(){
//        String target = this.getText().toString().trim();
//        int bl = common.badWords.length;
//        for(int i=0;i<bl;i++){
//            if(target.contains(common.badWords[i])){
//                Toast.makeText(
//                        common.context, "不能含有敏感词:"+ common.badWords[i],Toast.LENGTH_LONG).show();
//                return false;
//            }
//        }
//        return true;
//    }

    @Override
    public Editable getText(){
        return super.getText();
    }

    @Override
    public String toString() {
        return super.toString();
    }

}
